package GameMain.Model
{
	import GameMain.Game;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	import library.SocketData.SocketData;
	
	public class EntityMove extends Sprite
	{
		private static var MAP_STEP_Y:int = 410;			//地图调整坐标
		
		public static var FACE_RIGHT:int = 1;			//面向右边
		public static var FACE_LEFT:int = 2;				//面前左边
		
		protected var _entityID:int = 0;							//实体ID
			
		private var _moveTimer:Timer = new Timer(40);
		
		protected var _face:int = 0;
		protected var _vx:Number = 0;
		protected var _vy:Number = 0;
		protected var _ax:Number = 0;
		protected var _ay:Number = 0;
		private var _startMovieTime:int = 0;
		private var _startX:Number = 0;
		private var _startY:Number = 0;
		
		
		public function EntityMove()
		{
			super();
			this._face = FACE_RIGHT;
			this._moveTimer.addEventListener(TimerEvent.TIMER,this.onEntityMoveEvent);
		}
		
		public function get entityID():int
		{
			return this._entityID;
		}
		
		public function setEntityID(eid:int):void
		{
			this._entityID = eid;
		}
		
		
		public function onEntityMove(data:SocketData):void
		{
			this._vx = data.readInt16();
			this._vy = data.readInt16();
			this._ax = data.readInt16();
			this._ay = data.readInt16();
			this._startMovieTime = getTimer();
			this._startX = x;
			this._startY = MAP_STEP_Y - this.y;
			Game.EngineMgr.printLog("onEntityMove",_startX,_startY);
			this._moveTimer.start();
		}
		
		private function onEntityMoveEvent(e:Event):void
		{
			var nowtime:int = getTimer();
			var _time:Number = (nowtime - this._startMovieTime)/100;
			
			var _x:Number = _vx * _time + (_ax*(_time*_time))/2;
			var _y:Number = _vy * _time + (_ay*(_time*_time))/2;
			
			this.setPosition(this._startX+_x,this._startY + _y);
			this.setDirection(_time)
		}
		
		public function setVisible(b:Boolean):void
		{
			this.visible = b;
		}
		
		//设置坐标
		public function setPosition(_x:Number,_y:Number = 0):void
		{
			this.x = _x;
			this.y = MAP_STEP_Y - _y; 
		}
		
		public function onEntityStop(data:SocketData):void
		{
			this._moveTimer.stop();
			var _x:int = data.readInt16();
			var _y:int = data.readInt16();
			Game.EngineMgr.printLog("onEntityStop",_x,_y);
			this.setPosition(_x,_y);
		}
		
		//设置面向
		public function setFaceDirection(face:int):void
		{
			if(this._face == face)
				return;
			this._face = face;
			this.scaleX *= -1;
		}
		
		public function addToParent(_parent:DisplayObjectContainer):void
		{
			_parent.addChild(this);
		}
		
		public function destroy():void
		{
			if(this.parent != null)
			{
				this.parent.removeChild(this);
			}
			this._moveTimer.stop();
			this._moveTimer.removeEventListener(TimerEvent.TIMER,this.onEntityMoveEvent);
		}
		
		protected function setDirection(_time:Number):void
		{
			
		}
	}
}